﻿var edu = {
    /**
     * 学员添加成功回调函数
     */
    createStudentSuccess: function (result) {
        var table = $(this).data('table');
        if (!table) {
            fx.alert('请指定data-table属性');
            return;
        }
        var $modal = $(this).getFormModal();
        if (result.success) {
            fx.alert('保存成功');
            $modal.uimodal().close();
            $(table).uitable().reload();
        }
        else {
            var errorEl = $(this).data('errorEl');
            if (errorEl) {
                var $container = $($(this).uiform().options.container);
                $container.find(errorEl).html(result.message).show();
            }
            else {
                fx.alert(result.message);
            }
        }
    }
};

$(document).on('click.fx.ajax', '.ajaxaction', function () {
    var self = $(this);
    if (!self.data('url')) {
        fx.msg('请指定data-url属性');
        return false;
    }
    var $table;
    var table = self.data('table');
    if (table) {
        $table = $(table);
    } else {
        $table = self.closest('.uitable');
    }
    var ops = $.extend({
        confirm: self.data('confirm') || '确定要执行当前操作吗?',
        loadMask: true,
        loadMaskDelay: 200,
        loadMaskTarget: $table[0],
        loadMaskMessage: '正在执行,请稍等...'
    }, self.data());

    var _success = function (result) {
        fx.loadMask.hide(ops);
        if (result.success) {
            $table.uitable().reload();
            var callback = self.data('successCallback');
            if (callback) {
                if ($.isFunction(callback)) {
                    callback.call(self[0]);
                } else {
                    callback.toFunction().call(self[0]);
                }
            }
        }
        else {
            fx.alert(result.message);
        }
    };

    var ajaxOptions = {
        url: ops.url,
        type: 'post',
        cache: false,
        data: self.data('data'),
        dataType: 'json',
        beforeSend: function () {
            fx.loadMask.show(ops);
        },
        error: function (result) {
            fx.loadMask.hide(ops);
            fx.ajaxFailAlert(result);
        },
        // complete: function () {
        //
        // },
        success: _success
    };

    fx.confirm(ops.confirm, function (index) {
        layer.close(index);
        $.ajax(ajaxOptions);
    });

    return false;
});



function initKindEditor(selector) {
    var simpleTools =
        [
            'source', '|','formatblock', 'fontname', 'fontsize', 'lineheight', '|', 'forecolor', 'hilitecolor', 'bold', 'italic', 'underline', '|',
            'justifyleft', 'justifycenter', 'justifyright', 'insertorderedlist', 'insertunorderedlist', '|',
            'plainpaste', 'wordpaste', 'removeformat','table', 'image',
            'link', 'unlink', 'hr', 'insertfile'
        ];

    var ifrbady;
    function pastimg() {
        $("iframe").each(function (i, v) {
            otype = $(v).attr("type");
            if ($(v).attr("class") == "ke-edit-iframe") {
                $(v)[0].style.border = 0;
                ifrbady = $(v)[0].contentWindow.document.body;
                $(v)[0].contentWindow.document.body.id = "txtcontent";
            }
        });
        var imgReader = function (item) {
            var blob = item.getAsFile(),
                reader = new FileReader();


            reader.onload = function (e) {
                var img = new Image();


                img.src = e.target.result;
                document.body.appendChild(img);
                ifrbady.appendChild(img);
            };


            reader.readAsDataURL(blob);
        };


        ifrbady.addEventListener('paste', function (e) {
            var clipboardData = e.clipboardData,
                i = 0,
                items, item, types;
            if (clipboardData) {
                items = clipboardData.items;
                if (!items) {
                    return;
                }
                item = items[0];
                types = clipboardData.types || [];
                for (; i < types.length; i++) {
                    if (types[i] === 'Files') {
                        item = items[i];
                        break;
                    }
                }
                if (item && item.kind === 'file' && item.type.match(/^image\//i)) {
                    imgReader(item);
                }
            }
        });
    }
    var ops = {
        resizeType: 1,
        items: simpleTools,
        allowPreviewEmoticons: true,
        allowImageUpload: true,
        uploadJson: '/KindEditor/Upload',
        fileManagerJson: '/KindEditor/Manager',
        allowFileManager: true,
        bodyClass: 'article-content',
        afterBlur: function () {
            this.sync();
            $(this).prev('.ke-container').removeClass('focus');
        },
        afterFocus: function () {
            $(this).prev('.ke-container').addClass('focus');
        },
        afterChange: function () {
            $(this).change().hide();
        },
        afterCreate: function () {
            var self = this;
            KindEditor.ctrl(document, 13, function () {
                self.sync();
                KindEditor('form[name=frm_f]')[0].submit();
            });
            KindEditor.ctrl(self.edit.doc, 13, function () {
                self.sync();
                KindEditor('form[name=frm_f]')[0].submit();
            });
            pastimg();
        }
    };

    KindEditor.create(selector, ops);
}